//************************************************************
//    > File Name: reverse-words-in-a-string.cpp
//    > Author: davis
//    > Mail:  
//    > Created Time: 2017年03月20日 10:01:20
//***********************************************************

#include<bits/stdc++.h>
#include<string>
using namespace std;

string reverseWords(string s){
	reverse(s.begin(),s.end());
	int begin=0,end=0,len=0;
	cout<<"reverse string :"<<s<<endl;
	while((begin=s.find_first_not_of(" ",end))!=string::npos){
		if((end=s.find(" ",begin))==string::npos){
			end=s.length();			
		}
		cout<<"begin is "<<begin<<" end is "<<end<<endl;
		reverse(s.begin()+begin,s.begin()+end);
		move(s.begin()+begin,s.begin()+end,s.begin()+len);
		len+=end-begin;
		s[len++] = ' ';

		cout<<s<<"----len is "<<len<<endl;
	}
	s.resize(len ? len-1 : 0);
	return s;
}
int main()
{
	string s="a  b";
	string res=reverseWords(s);
	cout<<s<<endl;
	cout<<res<<endl;
}

